﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using NZBMatrixAdvancedFeedReader.Classes;

namespace NZBMatrixAdvancedFeedReader
{
    public partial class frmSearchTemplateMaintenance : Form
    {
        public frmSearchTemplateMaintenance()
        {
            InitializeComponent();
        }

        #region Event Handlers

        private void frmSearchTemplateMaintenance_Load(object sender, EventArgs e)
        {
            RefreshAll();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {

            Classes.SearchTemplate template = GetGridSelectedItem();
            if (template != null)
            {

                //  Validate
                if (template.Name.ToUpper() == "Default".ToUpper())
                {
                    CommonTasks.ShowErrorMesssage("You cannot delete the 'Default' template.");
                    return;
                }

                if (CommonTasks.ShowQuestionMessage("Are you sure you want to delete the template?", "Delete") == System.Windows.Forms.DialogResult.Yes)
                {
                    Classes.SystemSettings.SearchTemplateList.Remove(template);
                    Classes.SystemSettings.SaveSettings();
                }
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {

            frmSearchTemplateDefinition frm;
            frm = new frmSearchTemplateDefinition();
            frm.SearchTemplate = null;
            if (frm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                Classes.SystemSettings.SearchTemplateList.Add(frm.SearchTemplate);
                SystemSettings.SaveSettings();
                RefreshAll();
            }
            frm.Dispose();

        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            Classes.SearchTemplate template = GetGridSelectedItem();
            if (template != null)
            {
                frmSearchTemplateDefinition frm;
                frm = new frmSearchTemplateDefinition();
                frm.SearchTemplate = template;
                if (frm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    SystemSettings.SaveSettings();
                    RefreshCurrentRow();
                }
                frm.Dispose();
            }
        }

        #endregion

        #region Private Methods

        private void RefreshAll()
        {

            dgv.DataSource = null;
            dgv.DataSource = Classes.SystemSettings.SearchTemplateList;

            //  Fix column Header text
            foreach (DataGridViewColumn item in dgv.Columns)
            {
                item.HeaderText = AddSpacesToSentence(item.HeaderText);
            }
            //dgv.Columns.AsQueryable()

        }

        /// <summary>
        /// http://stackoverflow.com/questions/272633/add-spaces-before-capital-letters
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        private string AddSpacesToSentence(string text)
        {
            if (string.IsNullOrWhiteSpace(text))
                return "";
            StringBuilder newText = new StringBuilder(text.Length * 2);
            newText.Append(text[0]);
            for (int i = 1; i < text.Length; i++)
            {
                if (char.IsUpper(text[i]) && text[i - 1] != ' ' & !char.IsUpper(text[i-1]))
                    newText.Append(' ');
                newText.Append(text[i]);
            }
            return newText.ToString();
        }

        private void RefreshCurrentRow()
        {
            dgv.Refresh();
        }


        private Classes.SearchTemplate GetGridSelectedItem()
        {
            if (dgv.SelectedRows.Count == 0)
                return null;

            return dgv.SelectedRows[0].DataBoundItem as Classes.SearchTemplate;

        }

        #endregion
        
    }
}
